const axios = require('axios');

(async () => {
  try {
    // 1. 登录
    const loginResponse = await axios.post('http://localhost:3000/api/auth/login', {
      email: 'admin@campus.com', 
      password: 'admin123'
    });
    
    const authToken = loginResponse.data.token;
    const userId = loginResponse.data.user.id;
    console.log(`✅ 登录成功，用户ID: ${userId}`);
    
    // 2. 获取用户拥有的勋章
    const ownedResponse = await axios.get('http://localhost:3000/api/badges?filter=owned', {
      headers: { Authorization: `Bearer ${authToken}` }
    });
    
    console.log(`🎖️ 用户拥有的勋章数量: ${ownedResponse.data.badges.length}`);
    
    const ownedActivityIds = ownedResponse.data.badges.map(badge => badge.activity_id);
    console.log('已获得勋章的活动ID:', ownedActivityIds);
    
    // 3. 获取可获得勋章
    const availableResponse = await axios.get('http://localhost:3000/api/badges?filter=available', {
      headers: { Authorization: `Bearer ${authToken}` }
    });
    
    console.log(`🔓 API返回的可获得勋章数量: ${availableResponse.data.badges.length}`);
    
    // 4. 检查可获得勋章中是否包含已拥有的活动
    const availableActivityIds = availableResponse.data.badges.map(badge => badge.activity_id);
    const conflictIds = availableActivityIds.filter(id => ownedActivityIds.includes(id));
    
    console.log('可获得勋章中的活动ID:', availableActivityIds.slice(0, 10), '...(只显示前10个)');
    console.log('冲突的活动ID (同时在已拥有和可获得中):', conflictIds);
    
    if (conflictIds.length > 0) {
      console.log('❌ 发现冲突！以下活动既出现在已拥有又出现在可获得中:');
      conflictIds.forEach(activityId => {
        const ownedBadge = ownedResponse.data.badges.find(b => b.activity_id === activityId);
        const availableBadge = availableResponse.data.badges.find(b => b.activity_id === activityId);
        
        console.log(`活动 ${activityId}:`);
        console.log(`  已拥有勋章: ID=${ownedBadge.id}, 状态=${ownedBadge.status}`);
        console.log(`  可获得勋章: ID=${availableBadge.id}, 状态=${availableBadge.status}`);
      });
    } else {
      console.log('✅ 无冲突，筛选逻辑正确');
    }
    
  } catch (error) {
    console.error('检查失败:', error.message);
  }
})(); 